﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CADImport;
using CADImport.RasterImage;
using System.IO;

namespace CadTextChange_Demo
{
    public partial class Form1 : Form
    {
        #region Var
        public CADImage cadImage;
        public CADIterate cadParams;
        public bool found;
        public string searchingText;
        public string mstr;
        public ulong handle;
        public ulong prevHandle;
        public int layoutIndex;
        public int entityIndex;
        public List<CADEntity> Markers = new List<CADEntity>();
        #endregion

        public Form1()
        {
            InitializeComponent();
            Protection.Register("irDevelopers.com", "irDevelopers.com@gmail.com", "A1F08E31A4836D365457F7A3A965E8D05C37F85D018917A8D5A64447D6FB325C00CEA140F39D35E831031873A01BDA5AFB5B9E9C13D638F10B02B7A3792F364", false);
            cadViewerControl.SHXForm.AddPath(@"E:\Program Files\CAD.NET 14.1\CAD .NET 14\shx\");
            //CADImage cadImage = new CADImage();
            found = false;
            btnReplace.Enabled = false;
        }

        public void loadFile()
        {
            OpenFileDialog tempOpen = new OpenFileDialog();
            tempOpen.Filter = "CAD 图纸（.dwg）|*.dwg";
            tempOpen.Title = "打开文件";
            if (tempOpen.ShowDialog() == DialogResult.OK)
            {
                cadImage = CADImage.CreateImageByExtension(tempOpen.FileName);
                cadImage.LoadFromFile(tempOpen.FileName);
                cadViewerControl.LoadFile(tempOpen.FileName);
            }
        }

        public void exportFile()
        {
            SaveFileDialog tempSave = new SaveFileDialog();
            tempSave.Title = "导出文件";
            tempSave.Filter = "DXF 文件(.dxf)|*.dxf|PDF文件(.pdf)|*.pdf";
            if (tempSave.ShowDialog() == DialogResult.OK)
            {
                switch (tempSave.FilterIndex)
                {
                    case 1:
                        {
                            CADImport.Export.DirectCADtoDXF.CADtoDXF toDXF = new CADImport.Export.DirectCADtoDXF.CADtoDXF(cadImage);
                            toDXF.SaveToFile(tempSave.FileName);
                            break;
                        }
                    case 2:
                        {
                            CADImport.Export.CADToPDF toPDF = new CADImport.Export.CADToPDF(cadImage);
                            toPDF.SaveToFile(tempSave.FileName);
                            break;
                        }
                        MessageBox.Show("保存成功");
                }
            }

        }

        public bool FindText(string str)
        {
            found = false;
            searchingText = str;
           
            for (int i = 0; i < cadImage.Converter.Entities.Count; i++)
            {
                if (cadImage.Converter.Entities[i].EntType == EntityType.Text)
                {
                    if (((CADText)cadImage.Converter.Entities[i]).Text == searchingText)
                    {
                        found = true;
                        layoutIndex = i;
                        break;
                    }
                    else
                        found = false;
                }
                else if (cadImage.Converter.Entities[i].EntType == EntityType.MText)
                {
                    if (((CADMText)cadImage.Converter.Entities[i]).Text == searchingText)
                    {
                        found = true;
                        layoutIndex = i;
                        break;
                    }
                    else
                        found = false;
                }
                else
                    found = false;
            }
            return found;
            
        }

        public void refresh()
        {
            CADImport.Export.DirectCADtoDXF.CADtoDXF toDXF = new CADImport.Export.DirectCADtoDXF.CADtoDXF(cadImage);
            string tempPath = Path.GetTempFileName() + ".dxf";
            toDXF.SaveToFile(tempPath);
            cadImage = CADImage.CreateImageByExtension(tempPath);
            cadImage.LoadFromFile(tempPath);
            cadViewerControl.LoadFile(tempPath);
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("现在用到loadfile");
            loadFile();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!FindText(textBox1.Text))
            {
                MessageBox.Show("Text is not found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                btnReplace.Enabled = false;
            }
            else
            {
                MessageBox.Show("Text is found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                btnReplace.Enabled = true;
            }
        }

        private void btnReplace_Click(object sender, EventArgs e)
        {
            if (!found)
            {
                MessageBox.Show("Text is not found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                switch (cadImage.Converter.Entities[layoutIndex].EntType)
                {
                    case EntityType.Text:
                        ((CADText)cadImage.Converter.Entities[layoutIndex]).Text = textBox2.Text;
                        refresh();
                        break;
                    case EntityType.MText:
                        ((CADMText)cadImage.Converter.Entities[layoutIndex]).Text = textBox2.Text;
                        refresh();
                        break;
                }

                MessageBox.Show("Complete!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            exportFile();
        }

        public void changeImage()
        {
            int i = 0;
            while (i < cadImage.Converter.Entities.Count)
            {
                switch (cadImage.Converter.Entities[i].EntType)
                {
                    case EntityType.Text:
                        ((CADText)cadImage.Converter.Entities[i]).Text = "aaa";
                        break;
                    case EntityType.MText:
                        ((CADMText)cadImage.Converter.Entities[i]).Text= "bbb";
                        break;
                    default:
                        break;

                }
                i++;
            }

        }

        private void tableLayoutPanel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            changeImage();
            refresh();
            MessageBox.Show("Complete!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
